package top.infopub.mgr.bpa.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.enums.bpa.BpaProcessDetailTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.BpaContractBpaReviewDao;
import top.infopub.mgr.bpa.dao.ContractEntryDao;
import top.infopub.mgr.bpa.domain.ContractDropDomain;
import top.infopub.mgr.bpa.domain.ContractEntryDomain;
import top.infopub.mgr.bpa.jsonVo.ContractEntryJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.ContractEntryService;
import top.infopub.mgr.bpa.vo.ContractEntryVo;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.model.bpa.BpaContractListing;
import top.infopub.model.bpa.ContractEntry;
import top.infopub.security.vo.AuthorizationVO;

import java.util.List;


@Service
public class ContractEntryServiceImpl extends GenericServiceImpl<ContractEntry, String> implements ContractEntryService {

    @Autowired
    private ContractEntryDao contractEntryDao;

    @Autowired
    private BpaContractBpaReviewDao bpaContractBpaReviewDao;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Override
    public GenericDao<ContractEntry, String> getDao() {
        return contractEntryDao;
    }

    @Override
    public Page<ContractEntryDomain> selectByDomainWhere(Page<ContractEntryDomain> page,
                                                         ContractEntryVo contractEntryVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ContractEntryDomain>)contractEntryDao.selectByDomainWhere(contractEntryVo);
    }

    @Override
    public List<ContractDropDomain> selectReview(String proCode) {
        return contractEntryDao.selectReview(proCode);
    }

    @Override
    public List<ContractDropDomain> selectExt(String proCode) {
        return contractEntryDao.selectExt(proCode);
    }

    @Override
    public RspVo queryEntryDetail(String id) {
        ContractEntryJsonVo jsonVo = new ContractEntryJsonVo();
      //查询主表数据
      ContractEntry contractEntry = contractEntryDao.selectById(id);
        jsonVo.setContractEntry(contractEntry);
        //子表数据
        if(StringUtils.isNotBlank(contractEntry.getContractId())) {
        List<BpaContractListing> listings = contractEntryDao.selectByContractId(contractEntry.getContractId());
        jsonVo.setBpaContractListings(listings);
        }

      return RspVo.getSuccessResponse(jsonVo);
    }

    @Override
    @Transactional
    public RspVo saveAll(Integer submitType, String bodyStr, ContractEntry contractEntry, List<BpaContractListing> bpaContractListings, AuthorizationVO userDtl) {
        //处理主表数据
        if (StringUtils.isNotBlank(contractEntry.getId())){
            contractEntryDao.updateByPrimaryKey(contractEntry);
        }else{
            contractEntryDao.insert(contractEntry);
        }
        //处理子项数据
        bpaContractBpaReviewDao.deleteListing(contractEntry.getContractId(),contractEntry.getContractType());
        int seq = 0;
        for (BpaContractListing bpaContractListing : bpaContractListings) {
            bpaContractListing.setContractId(contractEntry.getContractId());
            bpaContractListing.setType(contractEntry.getContractType());
            bpaContractListing.setSeq(seq++);
        }
        if (submitType==2){
            bpaProcessManageService.sendProcess(BpaProcessDetailTypeEnum.PRO_NODE_TYPE6, bodyStr, contractEntry.getId(), contractEntry.getProCode(), userDtl);
        }
        return RspVo.getSuccessResponse();
    }

    @Override
    public ContractEntry selectBySguid(String id) {
        return contractEntryDao.selectById(id);
    }
}
